import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

//import javax.swing.*;
import javax.swing.filechooser.*;

/* ImageFilter.java is a 1.4 example used by FileChooserDemo2.java. */
public class CustomFileFilter extends FileFilter {

    private ArrayList valid = new ArrayList();
    
    //Accept all directories and all gif, jpg, tiff, or png files.
    public boolean accept(File f) {
        if (f.isDirectory()) {
            return true;
        }
        String ext = getExtension(f);
        if (ext != null && extensionValid(ext))
        	return true;
        return false;
    }
    
    private boolean extensionValid(String ext)
    {
        if (valid.contains(ext))
            return true;
        return false;
    }
    
    public String getExtension(File f) 
    { 
        String ext = null; 
        String s = f.getName(); 
        int i = s.lastIndexOf('.'); 
        if (i > 0 && i < s.length() - 1) { 
            ext = s.substring(i+1).toLowerCase(); 
        } 
        return ext; 
     } 

    public void addExtension(String ext)
    {
        valid.add(ext);
    }
    
    //The description of this filter
    public String getDescription() 
    {
        if(!valid.isEmpty()){
            Iterator it = valid.iterator();
            String exts = "*." + (String) it.next();
            while(it.hasNext()){
                exts = exts + ", *." + (String) it.next();
            }
            return (exts);
        }
        return "Folders only";
    }
}
